package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

	// 分页查询用
	@Autowired
	private RoomInfoMapper roomInfoMapper;

	// 做回显用 用mapper
	// 所属公寓
	@Autowired
	private ApartmentInfoMapper apartmentInfoMapper;

	// 房间属性信息
	@Autowired
	private AttrValueMapper attrValueMapper;

	// 配套信息
	@Autowired
	private FacilityInfoMapper facilityInfoMapper;

	// 标签信息
	@Autowired
	private LabelInfoMapper labelInfoMapper;

	// 支付方式信息
	@Autowired
	private PaymentTypeMapper paymentTypeMapper;

	// 可选租期信息
	@Autowired
	private LeaseTermMapper leaseTermMapper;

	// 图片信息
	@Autowired
	private GraphInfoMapper graphInfoMapper;




	// 做新增跟修改操作用 因为要批量操作数据 所以用service 且操作的是关系表
	// 房间配套关系
	@Autowired
	private RoomFacilityService roomFacilityService;

	// 房间标签关系
	@Autowired
	private RoomLabelService roomLabelService;

	// 房间属性关系
	@Autowired
	private RoomAttrValueService roomAttrValueService;

	// 房间支付方式管理
	@Autowired
	private RoomPaymentTypeService roomPaymentTypeService;

	// 房间租期关系表
	@Autowired
	private RoomLeaseTermService roomLeaseTermService;

	// 图片表
	@Autowired
	private GraphInfoService graphInfoService;


	// 下方的mapper是进行房间删除做判断用的
	// 租约信息表 里面带status
	@Autowired
	private LeaseAgreementMapper leaseAgreementMapper;

	// 分页查询完成
	@Override
	public IPage<RoomItemVo> pageItem(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
		return roomInfoMapper.pageItem(roomItemVoPage,queryVo);
	}


	// 获取房间详细信息 （回显操作 所以只需要引用mapper）
	// 传入的是房间ID
	@Override
	public RoomDetailVo getDetailById(Long id) {
		RoomDetailVo roomDetailVo = new RoomDetailVo();

		// 查询RoomInfo的基本信息
		RoomInfo roomInfo = this.getById(id);
		// 这里注意给公寓mapper传入的应该是roominfo对应的公寓id
		ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());

		List<AttrValueVo> attrValueVoList =  attrValueMapper.selectListByRoomId(id);
		List<FacilityInfo> facilityInfoList =  facilityInfoMapper.selectListByRoomId(id);
		List<LabelInfo> labelInfoList =  labelInfoMapper.selectListByRoomId(id);
		List<PaymentType> paymentTypeList =  paymentTypeMapper.selectListByRoomId(id);
		List<LeaseTerm> leaseTermList =  leaseTermMapper.selectListByRoomId(id);
		List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndItemId(id, ItemType.ROOM);


		BeanUtils.copyProperties(roomInfo, roomDetailVo);
		roomDetailVo.setApartmentInfo(apartmentInfo);
		roomDetailVo.setAttrValueVoList(attrValueVoList);
		roomDetailVo.setFacilityInfoList(facilityInfoList);
		roomDetailVo.setLabelInfoList(labelInfoList);
		roomDetailVo.setPaymentTypeList(paymentTypeList);
		roomDetailVo.setLeaseTermList(leaseTermList);
		roomDetailVo.setGraphVoList(graphVoList);

		return roomDetailVo;
	}


	// 自定义新增/修改功能
	@Override
	public void saveOrUpdateInfo(RoomSubmitVo roomSubmitVo) {
		Long id = roomSubmitVo.getId();

		// 先添加/修改房间基本信息
		this.saveOrUpdate(roomSubmitVo);
		// 判断id是否为null
		if(id!=null){
			// 那就是修改操作
			// 先清空关系表
			roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
			roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
			roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
			roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
			roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
			graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType,ItemType.ROOM));

		}


		// 房间配套信息关系添加/修改 实现
		List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
		ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
		if(!CollectionUtils.isEmpty(facilityInfoIds)){
			for (Long facilityInfoId : facilityInfoIds) {
				RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
				roomFacilities.add(roomFacility);
			}
			roomFacilityService.saveBatch(roomFacilities);
		}

		// 房间标签信息关系添加/修改 实现
		List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
		ArrayList<RoomLabel> roomLabels = new ArrayList<>();
		if(!CollectionUtils.isEmpty(labelInfoIds)){
			for (Long labelInfoId : labelInfoIds) {
				RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
				roomLabels.add(roomLabel);
			}
			roomLabelService.saveBatch(roomLabels);
		}

		// 房间属性信息关系添加/修改 实现
		List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
		ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>();
		if(!CollectionUtils.isEmpty(attrValueIds)){
			for (Long attrValueId : attrValueIds) {
				RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
				roomAttrValues.add(roomAttrValue);
			}
			roomAttrValueService.saveBatch(roomAttrValues);

		}

		// 房间支付方式信息关系添加/修改 实现
		List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
		ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
		if(!CollectionUtils.isEmpty(paymentTypeIds)){
			for (Long paymentTypeId : paymentTypeIds) {
				RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
				roomPaymentTypes.add(roomPaymentType);
			}
			roomPaymentTypeService.saveBatch(roomPaymentTypes);
		}

		// 房间租期信息关系添加/修改 实现
		List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
		ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
		if(!CollectionUtils.isEmpty(leaseTermIds)){
			for (Long leaseTermId : leaseTermIds) {
				RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
				roomLeaseTerms.add(roomLeaseTerm);
			}
			roomLeaseTermService.saveBatch(roomLeaseTerms);
		}


		// 图片信息添加/修改 实现
		List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
		ArrayList<GraphInfo> graphInfos = new ArrayList<>();
		if(!CollectionUtils.isEmpty(graphVoList)){
			for (GraphVo graphVo : graphVoList) {
				GraphInfo graphInfo = new GraphInfo();
				graphInfo.setItemId(roomSubmitVo.getId());
				graphInfo.setItemType(ItemType.ROOM);
				String url = graphVo.getUrl();
				String fileName = url.substring(url.lastIndexOf("/")+1);
				graphInfo.setUrl(url);
				graphInfo.setName(fileName);
				graphInfos.add(graphInfo);
			}
			graphInfoService.saveBatch(graphInfos);
		}

	}


	// 1. 删除之前得先判断房间是否正在入住 而入住状态就需要在Lease_agreement表中的status查询

	@Override
	public void removeByIdInfo(Long id) {

		// 查询其中具体的一个房间 所以用selectOne
		LeaseAgreement leaseAgreement = leaseAgreementMapper.selectOne(new LambdaQueryWrapper<LeaseAgreement>().eq(LeaseAgreement::getRoomId, id));
		if(leaseAgreement!= null){
			// 说明有房 提示不能删除
			throw new LeaseException(555, "当前房间已被租出，暂时不能删除！");
		}
		// 删除房间基本信息
		this.removeById(id);

		// 删除与房间附属的相关信息(直接copy前面新增/修改代码进行复用)
		roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
		roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
		roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
		roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
		roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
		graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType,ItemType.ROOM));

	}

	@Override
	public List<RoomInfo> listInfo(Long id) {

		return roomInfoMapper.listInfo(id);
	}
}




